package com.code.zjm.demo.adapt;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private ListView lvDemo;
    private ArrayAdapter<ListCellData> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.floatBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, BaseAdapterDemo.class);
                startActivity(i);
            }
        });

//        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);//使用系统提供的资源
        adapter = new ArrayAdapter<>(this, R.layout.subview_list);//使用自定义资源
        lvDemo = (ListView) findViewById(R.id.lv_demo);
        lvDemo.setAdapter(adapter);

        adapter.add(new ListCellData(01, "Lin", "man", 18));
        adapter.add(new ListCellData(02, "May", "woman", 20));

        lvDemo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListCellData listCellData = adapter.getItem(position);
                if (listCellData != null) {
                    Toast.makeText(MainActivity.this, String.format(Locale.CHINA,
                            "姓名：%s - 性别：%s - 年龄：%d",
                            listCellData.getUserName(), listCellData.getSex(), listCellData.getAge()),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
